var Session = require('../../vendor/qcloud-weapp-client-sdk/lib/session.js');
var config = require('../../config.js');
var commons = require('../../commons.js');
var qcloud = require('../../vendor/qcloud-weapp-client-sdk/index');
var register = require('../../template/register/register.js');
var utils = require('../../vendor/qcloud-weapp-client-sdk/lib/utils.js');
var index = {

  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: [],
    indicatorDots: true,
    autoplay: false,
    interval: 5000,
    duration: 1000,
    dataOver: false, //下划线
    page: 1, //分页
    listData: [],
    navListData: '',
    lxIdNav: "",
    currentIndex: 0,
    isOpenData: 0,
    guanFanData: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const scene = decodeURIComponent(options.scene);
    // const scene = "../details/details?ia=1188";
    console.log("scene值是：" + scene);
    if (scene != '' && scene != 'undefined') {
      wx.navigateTo({
        url: scene,
      })
    }
    wx.showLoading({
      title: '请稍后',
    })
    // this.GuanfangTouxiang();
    // banner图请求
    this.GuanFanList();
    // 首页分类
    this.indexCat();

  },
  //选项卡切换
  indexNavClick: function (e) {
    var id = e.currentTarget.id;
    var lx = this.data.navListData[id].cat_id;
    this.setData({
      listData: [],
      lxIdNav: lx,
      currentIndex: id,
      page: 1
    });
    this.indexListData()
  },
  // 首页列表数据请求
  indexListData: function () {
    var that = this;
    qcloud.request({
      login: false,
      url: config.service.indexDataUrl,
      data: {
        page: that.data.page,
        cat_id: this.data.lxIdNav,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var arr = that.data.listData;
        arr = arr.concat(that._formatData(res.data.data.data));
        if (that.data.listData.length !== 0) {
          if (res.data.data.data.length == 0) {
            // 分页没有数据了，显示提示框
            that.setData({
              dataOver: true
            })
          } else {
            that.setData({
              page: that.data.page + 1,
              listData: arr,
            });
          }
        } else {
          that.setData({
            listData: arr,
            page: that.data.page + 1,
          })
        }
        that.isopen();
      },
      fail: function (res) {
        utils.showError('请求出错')
      },
      complete: function () {
        setTimeout(function () {
          wx.hideLoading();
          wx.stopPullDownRefresh();
        }, 500)
      }
    });

  },
  // 轮播图跳转
  bannerClickUrl: function (e) {
    var id = e.currentTarget.id;
    var csl_url = this.data.imgUrls[id].csl_url;
    wx.navigateTo({
      url: csl_url,
    })
  },
  // 跳到视频详情页
  goToDetails: function (e) {
    var id = e.currentTarget.id;
    var detailID = this.data.listData[id].id;
    wx.navigateTo({
      url: '../details/details?ia=' + detailID,
    })
  },
  // 开关
  isopen() {
    var that = this;
    qcloud.request({
      login: false,
      url: config.service.isOpenUrl,
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        that.setData({
          isOpenData: res.data.data.fieldValue
        })
      },
      fail: function (res) {
        utils.showError('请求出错')
      },
      complete: function () {

      }
    });
  },
  //banner图请求
  GuanFanList: function () {
    var that = this;
    // banner请求地址
    qcloud.request({
      login: true,
      url: config.service.carouselUrl,
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        that.setData({
          imgUrls: res.data.data
        });
        // that.GuanfangTouxiang();
        // that.isopen();
      },
      fail: function (res) {
        utils.showError(res.message)
      },
      complete: function () {
        setTimeout(function () {
          wx.hideLoading();
          wx.stopPullDownRefresh();
        }, 500)
      }
    });

  },
  //首页分类
  indexCat: function () {
    var that = this;
    qcloud.request({
      login: true,
      url: config.service.indexCatUrl,
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        that.setData({
          lxIdNav: res.data.data[0].cat_id,
          navListData: res.data.data
        });

      },
      fail: function (res) {
        utils.showError(res.message)
      },
      complete: function () { }
    });

  },
  // GuanfangTouxiang: function() {
  //   var that = this;
  //   //请求官方头像和数量
  //   qcloud.request({
  //     login: false,
  //     url: config.service.officialUrl,
  //     header: {
  //       'content-type': 'application/json' // 默认值
  //     },
  //     success: function(res) {
  //       that.setData({
  //         guanFanData: res.data.data
  //       });
  //     },
  //     fail: function(res) {
  //       utils.showError(res.message)
  //     },
  //     complete: function() {
  //       setTimeout(function() {
  //         wx.hideLoading();
  //         wx.stopPullDownRefresh();
  //       }, 500)
  //     }
  //   });
  // },
  // 点击关注
  clickGuanZhu: function () {
    var room_id = this.data.guanFanData.info.userId;
    var that = this;
    qcloud.request({
      login: true,
      url: config.service.collAddUrl,
      data: {
        type_id: 1,
        room_id: room_id
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        utils.showError(res.data.data.msg);
        that.GuanfangTouxiang();
        // that.liveToom();
      },
      fail: function (res) {
        utils.showError(res.message)
      },
      complete: function () {
        setTimeout(function () {
          wx.hideLoading();
          wx.stopPullDownRefresh();
        }, 500)
      }
    });
  },
  //时间转换
  _formatData: function (dataArray) {
    var that = this;
    for (var i in dataArray) {
      dataArray[i].stime = commons.dateTools.timestampToDataDayTwo(dataArray[i].stime);
      dataArray[i].click = commons.dateTools.wanNumZh(dataArray[i].click);
    }
    return dataArray;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      currentIndex: 0,
      page: 1,
      listData: []
    })
    this.indexCat();
    this.GuanFanList();
    this.indexListData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      currentIndex: 0,
      page: 1,
      listData: []
    })
    this.indexCat();
    this.GuanFanList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.indexListData();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
};
var data = Object.assign(index.data, register.data);
var page = Object.assign(index, register);
page.data = data;
Page(page);